package com.dts.yjnb.service.controller.admin;

import com.dts.common.core.controller.BaseController;
import com.dts.common.core.domain.AjaxResult;
import com.dts.common.core.page.TableDataInfo;
import com.dts.yjnb.service.domain.Agricultural;
import com.dts.yjnb.service.domainDto.AgriculturalDTO;
import com.dts.yjnb.service.service.IAgriculturalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 农事服务Controller
 *
 * @author dts
 */
@RestController
@RequestMapping("/yjnb/agricultural")
public class AgriculturalController extends BaseController
{
    @Autowired
    private IAgriculturalService agriculturalService;

    /**
     * 查看农事服务列表
     */
        @GetMapping("/list")
    public TableDataInfo list(AgriculturalDTO agriculturalDTO)
    {
        return agriculturalService.pageQuery(agriculturalDTO);
    }

    /**
     * 搜索农事服务信息
     * 支持按标题或摘要进行模糊搜索
     */
    @GetMapping("/search")
    public TableDataInfo search(AgriculturalDTO agriculturalDTO)
    {
        return agriculturalService.search(agriculturalDTO);
    }

    /**
     * 根据ID查询农事服务详情
     */
    @GetMapping("/get/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(agriculturalService.getByid(id));
    }

    /**
     * 新增农事服务信息
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody AgriculturalDTO agriculturalDTO)
    {
        if (agriculturalService.add(agriculturalDTO))
        {
            return AjaxResult.success("新增成功");
        }
        return AjaxResult.error("新增失败");
    }

    /**
     * 修改农事服务信息
     */
    @PutMapping("/update")
    public AjaxResult update(@RequestBody AgriculturalDTO agriculturalDTO)
    {
        if (agriculturalService.update(agriculturalDTO))
        {
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    /**
     * 批量删除农事服务信息
     * 支持单个删除和批量删除
     */
    @DeleteMapping("/deletes")
    public AjaxResult delete(@RequestParam List<Long> ids)
    {
        agriculturalService.deleteByids(ids);
        return AjaxResult.success("删除成功");
    }

    /**
     * 批量修改农事服务状态
     */
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody Agricultural agricultural)
    {
        List<Long> idList = Arrays.asList(agricultural.getIds());
        agriculturalService.updateStatusByids(idList, agricultural.getStatus());
        return AjaxResult.success("状态更新成功");
    }

    /**
     * 批量修改农事服务推荐状态
     */
    @PutMapping("/updateRecommend")
    public AjaxResult updateRecommend(@RequestBody Agricultural agricultural)
    {
        List<Long> idList = Arrays.asList(agricultural.getIds());
        agriculturalService.updateRecommendByids(idList, agricultural.getIsRecommend());
        return AjaxResult.success("推荐状态更新成功");
    }
}
